//
//  dynamicViewController.m
//  theCoupleLive
//
//  Created by USER on 2016/12/5.
//  Copyright © 2016年 USER. All rights reserved.
//

#import "dynamicViewController.h"
#import "coupleLiveAPI.h"
#import "mapVIew.h"
#import "timeModel.h"
#import "publishViewController.h"
//#import <MapKit/MapKit.h>
#import "AppDelegate.h"
#define APPDELEGATE ((AppDelegate*)[[UIApplication sharedApplication] delegate])
@interface dynamicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)mapVIew *mapview;
@property (nonatomic,strong)UIImageView *headImageView;
@property (nonatomic,strong)UILabel *labelName;
@property (nonatomic,strong)UILabel *labelTime;
@property (nonatomic,strong)UIImageView *timeImg;//时间
@property (nonatomic,strong)UILabel *distancelabel;
@property (nonatomic,strong)UIImageView *distanceImg;//距离
@property (nonatomic,strong)UIView *view1;//存放性别年龄的view
@property (nonatomic,strong)UIImageView *sexImageView;
@property (nonatomic,strong)UILabel *labelAge;
@property (nonatomic,strong)UIImageView *sexImageView2;
@property (nonatomic,strong)UILabel *labelAge2;
@property (nonatomic,strong)UIView *view2;//放等级的view
@property (nonatomic,strong)UILabel *labelContent;//说说内容
@property (nonatomic,strong)UIView *topCellView;//cell之间的间隔
@property (nonatomic,strong)NSMutableArray *imagesArray;
@property (nonatomic,strong)UIScrollView *scrollView;

//@property (nonatomic,strong)UIButton *button1;//点赞
@property (nonatomic,strong)UIButton *button2;//评论
@property (nonatomic,strong)UIButton *button3;//转发
//@property (nonatomic,strong)UILabel *num1;//点赞数量
@property (nonatomic,strong)UILabel *num2;//评论数量
@property (nonatomic,strong)UILabel *num3;//转发数量
@property (nonatomic,strong)UIView *view3;//放点赞评论转发的view

@property (nonatomic,strong)NSArray *dynamicArr;//
@property (nonatomic,strong)UIButton *btn_publish;
@end

@implementation dynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.view.backgroundColor=[UIColor orangeColor];
    self.tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-64)];
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
    [self.view addSubview:self.tableview];
    
    //发布动态
    self.btn_publish=[[UIButton alloc]initWithFrame:CGRectMake(290, 410, 50, 50)];
    [self.btn_publish setBackgroundImage:[UIImage imageNamed:@"fabudongtai"] forState:UIControlStateNormal];
    self.btn_publish.layer.masksToBounds=YES;
    self.btn_publish.layer.cornerRadius=25;
    [self.view addSubview:self.btn_publish];
    [self.btn_publish addTarget:self action:@selector(turnToPublisg) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableview.tableFooterView = [UIView new];
    self.imagesArray=[[NSMutableArray alloc]initWithObjects:@"QQ", nil];
    self.dynamicArr=[[NSArray alloc]init];
    [coupleLiveAPI nearbyDynamicWithuid:APPDELEGATE.user.userid ugps:@"31.980942,120.9195" address:@"南通市" success:^(id response) {
//        NSLog(@"%@",[response count]);
        
        self.dynamicArr=response;
        NSLog(@"%@",self.dynamicArr);
        [self.tableview reloadData];
    }];
//    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(100, 150, 60, 60)];
//    [button setTitle:@"text" forState:UIControlStateNormal];
//    
//    [self.view addSubview:button];
//    [button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    
    // Do any additional setup after loading the view.
}

#pragma mark 跳转发表动态
-(void)turnToPublisg{
    publishViewController *pubVC=[[publishViewController alloc]init];
    [self.navigationController pushViewController:pubVC animated:YES];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dynamicArr.count+1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //indexPath.row==0 地图
    if (indexPath.row==0) {
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellid"];
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"];
//            //地图
//            self.mapview=(mapVIew *)[self.view viewWithTag:100];
//            self.mapview=[[mapVIew alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 130)];
//            self.mapview.tag=100;
//            [cell addSubview:self.mapview];
            cell.backgroundColor=[UIColor orangeColor];
            [cell setFrame:CGRectMake(0, 0, self.view.frame.size.width,130)];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    //其他
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid1"];
    if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid1"];
            
            //cell之间的间隔
            self.topCellView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 3)];
            self.topCellView.backgroundColor=[UIColor colorWithRed:230.0/255 green:230.0/255 blue:230.0/255 alpha:1];
            self.topCellView.tag=8;
            
            //头像
            self.headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(13, 21.5f, 59, 59)];
            self.headImageView.tag=1;
            self.headImageView.layer.masksToBounds=YES;
            self.headImageView.layer.cornerRadius=30;
            
            //名称
            self.labelName=[[UILabel alloc]initWithFrame:CGRectMake(86, 30, self.view.frame.size.width-84-86, 20)];
            self.labelName.tag=2;
            
            //性别
            self.sexImageView=[[UIImageView alloc]initWithFrame:CGRectMake(2, 1, 12,12)];
            self.sexImageView.tag=3;
            
            //年龄
            self.labelAge=[[UILabel alloc]initWithFrame:CGRectMake(14, 0, 14, 13)];
            [self.labelAge setTextColor:[UIColor whiteColor]];
            self.labelAge.font=[UIFont systemFontOfSize:9];
            self.labelAge.tag=4;
            
            //存放性别年龄的view
            self.view1=[[UIView alloc]initWithFrame:CGRectMake(87.5f, 55, 28, 13)];
            [self.view1 setBackgroundColor:[UIColor colorWithRed:254.0/255 green:179.0/255 blue:197.0/255 alpha:1]];
            self.view1.layer.cornerRadius=6.5;
            [self.view1 addSubview:self.sexImageView];
            [self.view1 addSubview:self.labelAge];
            self.view1.tag=5;
        
        //等级星星
        self.sexImageView2=[[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 8,8)];
        self.sexImageView2.tag=30;
        
        //等级
        self.labelAge2=[[UILabel alloc]initWithFrame:CGRectMake(12, 0, 21, 13)];
        [self.labelAge2 setTextColor:[UIColor whiteColor]];
        self.labelAge2.font=[UIFont systemFontOfSize:9];
        self.labelAge2.tag=40;
        
        //存放等级的view
        self.view2=[[UIView alloc]initWithFrame:CGRectMake(121, 55, 35, 13)];
        self.view2.layer.cornerRadius=6.5;
        [self.view2 setBackgroundColor:[UIColor colorWithRed:146.0/255 green:85.0/255 blue:250.0/255 alpha:1]];
        [self.view2 addSubview:self.sexImageView2];
        [self.view2 addSubview:self.labelAge2];
        self.view2.tag=50;
        
        //时间图标
        self.timeImg=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-72-12, 34, 14, 14)];
        self.timeImg.tag=60;
        [cell.contentView addSubview:self.timeImg];
        
        //时间
            self.labelTime=[[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-65, 34, 65, 14)];
            self.labelTime.tag=6;
        self.labelTime.font=[UIFont systemFontOfSize:11];
        self.labelTime.textColor=[UIColor lightGrayColor];
        
        //距离图片
        self.distanceImg=[[UIImageView alloc]init];
        self.distanceImg.tag=70;
        [cell.contentView addSubview:self.distanceImg];
        
        //距离
        self.distancelabel=[[UILabel alloc]init];
        self.distancelabel.tag=80;
        self.distancelabel.font=[UIFont systemFontOfSize:11];
        self.distancelabel.textColor=[UIColor lightGrayColor];
        [cell.contentView addSubview:self.distancelabel];
        
            //内容
            self.labelContent=[[UILabel alloc]init];
            self.labelContent.tag=7;
            self.labelContent.numberOfLines=0;
            self.labelContent.font=[UIFont systemFontOfSize:14];
            self.labelContent.textColor=[UIColor colorWithRed:105.0/255 green: 105.0/255 blue:105.0/255 alpha:1];
        
            //滚动视图
            self.scrollView=[[UIScrollView alloc]init];
            self.scrollView.tag=9;
        
            //点赞评论转发的view
            self.view3=[[UIView alloc]init];
            self.view3.tag=11;
            
            //评论
            self.button2=[[UIButton alloc]init];
            self.button2.tag=13;
            
            //转发
            self.button3=[[UIButton alloc]init];
            self.button3.tag=14;
            self.button2.frame=CGRectMake(240, 5, 15, 15);
            self.button3.frame=CGRectMake(296, 5, 15, 15);
            [self.view3 addSubview:self.button2];
            [self.view3 addSubview:self.button3];
            
            //点赞评论转发d的数量
            self.num2=[[UILabel alloc]initWithFrame:CGRectMake(self.button2.frame.origin.x+self.button2.frame.size.width+5, 10, 35, 10)];
            self.num3=[[UILabel alloc]initWithFrame:CGRectMake(self.button3.frame.origin.x+self.button3.frame.size.width+5, 10, 45, 10)];
            self.num2.font=[UIFont systemFontOfSize:10];
            self.num3.font=[UIFont systemFontOfSize:10];
        self.num2.textColor=[UIColor colorWithRed:150.0/255 green:150.0/255 blue:150.0/255 alpha:1];
        self.num3.textColor=[UIColor colorWithRed:150.0/255 green:150.0/255 blue:150.0/255 alpha:1];
            self.num2.tag=16;
            self.num3.tag=17;
            [self.view3 addSubview:self.num2];
            [self.view3 addSubview:self.num3];
            
            //为scrollview创建图片
            CGFloat scrollviewWidth=[UIScreen mainScreen].bounds.size.width-26;
            int hang=[self.imagesArray count]%3==0? (int)[self.imagesArray count]/3:(int)[self.imagesArray count]/3+1;
            int imgwidth=(scrollviewWidth-10)/3;
            for (int i=0; i<hang; i++) {
                for (int j=0; j<3; j++) {
                    if (i*3+j==self.imagesArray.count) {
                        break;
                    }
                    UIButton *imgbutton=[[UIButton alloc]initWithFrame:CGRectMake(j*(imgwidth+5), i*(imgwidth+5), imgwidth, imgwidth)];
                    [imgbutton setImage:[UIImage imageNamed:[self.imagesArray objectAtIndex:i*3+j]] forState:UIControlStateNormal];
                    imgbutton.tag=i*3+j;
                    [imgbutton addTarget:self action:@selector(browser:) forControlEvents:UIControlEventTouchUpInside];
                    [self.scrollView addSubview:imgbutton];
                }
            }
            
            //        [self.viewAge addSubview:self.labelAge];
            [cell.contentView addSubview:self.headImageView];
            [cell.contentView addSubview:self.labelName];
            [cell.contentView addSubview:self.labelTime];
            //        [cell.contentView addSubview:self.imageSex];
            [cell.contentView addSubview:self.view1];
            [cell.contentView addSubview:self.labelContent];
            [cell.contentView addSubview:self.topCellView];
            [cell.contentView addSubview:self.scrollView];
            [cell.contentView addSubview:self.view2];
        [cell.contentView addSubview:self.view3];
        }
        else{
            //重用
            self.headImageView=(UIImageView *)[cell.contentView viewWithTag:1];
            self.labelName=(UILabel *)[cell.contentView viewWithTag:2];
            self.sexImageView=(UIImageView *)[cell.contentView viewWithTag:3];
            self.labelAge=(UILabel *)[cell.contentView viewWithTag:4];
            self.view1=(UIView *)[cell.contentView viewWithTag:5];
            self.sexImageView2=(UIImageView *)[cell.contentView viewWithTag:30];
            self.labelAge2=(UILabel *)[cell.contentView viewWithTag:40];
            self.view2=(UIView *)[cell.contentView viewWithTag:50];
            self.labelTime=(UILabel *)[cell.contentView viewWithTag:6];
            self.timeImg=(UIImageView *)[cell.contentView viewWithTag:60];
            self.distancelabel=(UILabel *)[cell.contentView viewWithTag:80];
            self.distanceImg=(UIImageView *)[cell.contentView viewWithTag:70];
            self.labelContent=(UILabel *)[cell.contentView viewWithTag:7];
            self.topCellView=(UIView *)[cell.contentView viewWithTag:8];
            self.scrollView=(UIScrollView *)[cell.contentView viewWithTag:9];
            self.view3=(UIView *)[cell.contentView viewWithTag:11];
            self.button2=(UIButton *)[cell.contentView viewWithTag:13];
            self.button3=(UIButton *)[cell.contentView viewWithTag:14];
            self.num2=(UILabel *)[cell.contentView viewWithTag:16];
            self.num3=(UILabel *)[cell.contentView viewWithTag:17];
        }
    //头像
    NSData *decodedImageData   = [[NSData alloc] initWithBase64Encoding:[[self.dynamicArr objectAtIndex:(indexPath.row-1)]objectForKey:@"img"]];
    UIImage *decodedImage      = [UIImage imageWithData:decodedImageData];
    self.headImageView.image=decodedImage;
    //名字
        self.labelName.text=[[self.dynamicArr objectAtIndex:(indexPath.row-1)]objectForKey:@"nickname"];
    //性别
    if ([[[self.dynamicArr objectAtIndex:(indexPath.row-1)]objectForKey:@"sex"]isEqualToString:@"男"]) {
        self.view1.backgroundColor=[UIColor colorWithRed:60.0/255 green:189.0/255 blue:252.0/255 alpha:1];
        self.sexImageView.image=[UIImage imageNamed:@"男"];
    }
    else{
        self.view1.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
        self.sexImageView.image=[UIImage imageNamed:@"女"];
    }
    //年龄
        self.labelAge.text=[NSString stringWithFormat:@"%@",[[self.dynamicArr objectAtIndex:(indexPath.row-1)]objectForKey:@"age"]];
    //时间
    self.timeImg.image=[UIImage imageNamed:@"time"];
    self.labelTime.text=[timeModel secondAgoWithTime:[NSString stringWithFormat:@"%@",[[self.dynamicArr objectAtIndex:(indexPath.row-1)]objectForKey:@"time"]]];
    //距离
    self.distanceImg.image=[UIImage imageNamed:@"map-fujin"];
    float m=[[[self.dynamicArr objectAtIndex:(indexPath.row-1)]objectForKey:@"distance"] floatValue];
    self.distancelabel.text=[NSString stringWithFormat:@"%.2fkm",m/1000];
    //等级
    self.labelAge2.text=[NSString stringWithFormat:@"%@级",[[self.dynamicArr objectAtIndex:(indexPath.row-1)]objectForKey:@"age"]];
    self.sexImageView2.image=[UIImage imageNamed:@"dengji"];
    //内容
    self.labelContent.text=[NSString stringWithFormat:@"%@",[[self.dynamicArr objectAtIndex:(indexPath.row-1)]objectForKey:@"content"]];
        //根据文字数量计算UILabelContent高度(已修改)
        CGFloat labelWidth=[UIScreen mainScreen].bounds.size.width-26;
        NSDictionary *attrs=@{NSFontAttributeName:self.labelContent.font};
        CGSize maxSize=CGSizeMake(labelWidth, MAXFLOAT);
        CGSize size=[self.labelContent.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
        self.labelContent.frame=CGRectMake(13, 89, size.width, size.height);
        
        //scrollview随着图片的增多而变高
        CGFloat scrollviewWidth=[UIScreen mainScreen].bounds.size.width-26;
        int hang=[self.imagesArray count]%3==0? (int)[self.imagesArray count]/3:(int)[self.imagesArray count]/3+1;
        int imgwidth=(scrollviewWidth-10)/3;
        //    self.scrollView.backgroundColor=[UIColor lightGrayColor];
        self.scrollView.frame=CGRectMake(13, 89+size.height+10, scrollviewWidth, (hang)*imgwidth+(hang-1)*5);
    
    self.distanceImg.frame=CGRectMake(25,89+size.height+(hang)*imgwidth+(hang-1)*5+15, 12, 15);
    self.distancelabel.frame=CGRectMake(self.distanceImg.frame.origin.x+self.distanceImg.frame.size.width+5,89+size.height+(hang)*imgwidth+(hang-1)*5+15, 250, 15);
        
        //点赞评论转发的view的位置
        self.view3.frame=CGRectMake(0, 89+size.height+(hang)*imgwidth+(hang-1)*5+15+20, self.view.frame.size.width, 30);
        [self.button2 setBackgroundImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
        [self.button3 setBackgroundImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
        
        //点赞评论转发的数量
        self.num2.text=@"赞(4)";
        self.num3.text=@"评论(1)";
        
        //    self.button1.contentMode=UIViewContentModeScaleAspectFit;
        //    self.button2.contentMode=UIViewContentModeScaleAspectFit;
        //    self.button3.contentMode=UIViewContentModeScaleAspectFit;
        //不显示选中颜色的方法
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setFrame:CGRectMake(0, 0, self.view.frame.size.width, 89+size.height+(hang)*imgwidth+(hang-1)*5+15+50)];
        return cell;

}

#pragma mark cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
    if (cell==nil) {
        return 0;
    }
    else{
        return cell.frame.size.height;
    }
}

//截下地图
//-(void)action{
//    CGRect rect = self.mapview.frame;
//    UIGraphicsBeginImageContext(rect.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    [self.mapview.layer renderInContext:context];
//    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    UIImageView *imgVIew=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,  self.view.frame.size.width,150)];
//                imgVIew.image=img;
//                [self.view addSubview:imgVIew];
//    [self.mapview removeFromSuperview];
//
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
